package com.cloudpivot.receivales;

import com.cloudpivot.receivales.contract.entity.erpcontract.Inventory;
import com.cloudpivot.receivales.contract.service.http.HttpAPIServiceForCrm;
import com.cloudpivot.receivales.contract.service.impl.InventoryServiceImpl;
import com.cloudpivot.receivales.crm.service.WeeklyCountServiceImpl;
import com.cloudpivot.receivales.invoice.bsweaverws.*;
import com.cloudpivot.receivales.invoice.pojo.KaiPiaoItemPOJO;
import com.cloudpivot.receivales.invoice.pojo.KaiPiaoPOJO;
import com.cloudpivot.receivales.invoice.services.OAWorkflowServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import javax.xml.rpc.ServiceException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@SpringBootTest
class CloudpivotApplicationTest4 {

    @Autowired
    private OAWorkflowServiceImpl oaWorkflowService;

    @Autowired
    private InventoryServiceImpl inventoryService;

    @Autowired
    private ObjectMapper objectMapper;

    @Test
    public void interfaceTest1() {
        KaiPiaoItemPOJO item = new KaiPiaoItemPOJO();
        item.setChanpinbm("JCFWF");
        item.setKehumc("测试推送客户");
        item.setZuizhongkh("测试推送最终客户");
        item.setXiangmumc("检测001");
        item.setBeizhu("测试推送");
        item.setShuliang(2.00);
        item.setDanjiahs(101.09);
        List<KaiPiaoItemPOJO> items = new ArrayList<>();
        items.add(item);
        KaiPiaoPOJO main = new KaiPiaoPOJO();
        main.setBiaoti("接口推送标题");
        main.setFapiaolx("增值税普票发票");
        main.setKaipiaogs("杭州博圣医学检验实验室有限公司");
        main.setKaipiaolb("检测外送");
        main.setShenqingr("15267167276");
        main.setShigouxyckd("0");
        main.setShoujianrxx("本人");
        main.setShenqingyy("测试接口用");
        main.setLiuchengwd(null);
        main.setItem(items);
        try {
            System.out.println(objectMapper.writeValueAsString(main));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    /**
     * webservice测试
     */
    @Test
    public void testpushworkflow() {

        //创建主表数据
        WorkflowRequestTableField[] wfrt = new WorkflowRequestTableField[2];
        wfrt[0] = new WorkflowRequestTableField();
        wfrt[0].setFieldName("crmid");
        wfrt[0].setFieldValue("springboot推送");
        wfrt[0].setView(true);
        wfrt[0].setEdit(true);

        wfrt[1] = new WorkflowRequestTableField();
        wfrt[1].setFieldName("wendangzt");
        wfrt[1].setFieldValue("springboot推送2");
        wfrt[1].setView(true);
        wfrt[1].setEdit(true);
        WorkflowRequestTableRecord[] wrtri = new WorkflowRequestTableRecord[1];
        wrtri[0] = new WorkflowRequestTableRecord();
        wrtri[0].setWorkflowRequestTableFields(wfrt);
        WorkflowMainTableInfo wmti = new WorkflowMainTableInfo();
        wmti.setRequestRecords(wrtri);

        //明细表开始
        int sRow = 2;//定义明细行数
        wrtri = new WorkflowRequestTableRecord[sRow];//
        for (int i = 0; i < sRow; i++) {
            wfrt = new WorkflowRequestTableField[2]; //字段信息
            wfrt[0] = new WorkflowRequestTableField();
            wfrt[0].setFieldName("mingxi1");//
            wfrt[0].setFieldValue("123");//
            wfrt[0].setView(true);//字段是否可见
            wfrt[0].setEdit(true);//字段是否可编辑
            wfrt[1] = new WorkflowRequestTableField();
            wfrt[1].setFieldName("mingxi2");//
            wfrt[1].setFieldValue("456");//
            wfrt[1].setView(true);//字段是否可见
            wfrt[1].setEdit(true);//字段是否可编辑
            wrtri[i] = new WorkflowRequestTableRecord();
            wrtri[i].setWorkflowRequestTableFields(wfrt);
        }
        //添加到明细表中
        WorkflowDetailTableInfo detail[] = new WorkflowDetailTableInfo[1];
        //指定明细表的个数，多个明细表指定多个，顺序按照明细的顺序
        detail[0] = new WorkflowDetailTableInfo();
        detail[0].setWorkflowRequestTableRecords(wrtri);
        //明细表配置完成

        //创建流程基本对象
        WorkflowBaseInfo wbi = new WorkflowBaseInfo();
        //创建流程基本信息对象
        WorkflowRequestInfo wri = new WorkflowRequestInfo();
        wri.setRequestName("springboot推送流程");//设置流程名称
        wbi.setWorkflowId("3222");
        wri.setCreatorId("1283");//创建人ID
        wri.setRequestLevel("0");//流程创建等级
        wri.setWorkflowMainTableInfo(wmti);//设置主表字段进去
        wri.setWorkflowDetailTableInfos(detail);//添加明细数据
        wri.setWorkflowBaseInfo(wbi);//设置基本流程基本数据
        wri.setIsnextflow("1");//是否提交到下个节点,如果是0的话,就在开始节点.

        String requestid = null;
        try {
            URL _url = new URL("http://172.16.0.201/services/WorkflowService");
            WorkflowServicePortType workflowServicePortType = new WorkflowServiceLocator().getWorkflowServiceHttpPort(_url);
            requestid = workflowServicePortType.doCreateWorkflowRequest(wri, 1273);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("放回结果:" + requestid);
    }

    @Test
    public void submitworkflw() {
        try {
            String rsp = null;
            URL _url = new URL("http://172.16.0.201/services/WorkflowService");
            WorkflowServicePortType workflowServicePortType = new WorkflowServiceLocator().getWorkflowServiceHttpPort(_url);
            WorkflowRequestInfo wfri = workflowServicePortType.getWorkflowRequest(387305, 1211, 0);
            System.out.println(wfri.getWorkflowHtmlShow().toString());
            rsp = workflowServicePortType.submitWorkflowRequest(wfri, 387305, 80, "submit", "for test");
            System.out.println(rsp);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    @Test
    public void interface5() {
        BigDecimal bd = new BigDecimal(0E-10);

        System.out.println("----------"+bd.toString());
        System.out.println("------+++----"+BigDecimal.ZERO);
    }

    @Test
    public void interface6(){
        Inventory inventory= inventoryService.getInventory("CP01010");
        if(inventory==null){
            System.out.println("null");
        }else{
            System.out.println(inventory.getCInvCCode());
        }
    }
}
